package com.eckey.lab.controller;

import com.alibaba.fastjson.JSON;
import com.eckey.lab.config.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;

/**
 * @Author: Marin
 * @CreateTime: 2023-10-08  15:24
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/socket")
public class WebSocketController {

    @Autowired
    private WebSocketServer webSocketServer;

    @GetMapping(path = "/publish/{userId}")
    public String publish(@PathVariable("userId") String userId, String message) throws IOException {
        webSocketServer.sendOneMessage(userId, message);
        log.info("信息发送成功！userId:{},message:{}", userId, message);
        HashMap maps = new HashMap();
        maps.put("code", "0");
        maps.put("msg", "success");
        return JSON.toJSONString(maps);
    }

}
